package com.gimi.cloud.jimi.manage.controller.prod;

import com.gimi.cloud.jimi.common.enums.ResponseCodeEnum;
import com.gimi.cloud.jimi.common.utils.JsonUtil;
import com.gimi.cloud.jimi.common.utils.Page;
import com.gimi.cloud.jimi.common.utils.UUIDUtil;
import com.gimi.cloud.jimi.common.utils.oss.AliOSSUtil;
import com.gimi.cloud.jimi.common.utils.reqhead.Response;
import com.gimi.cloud.jimi.common.utils.reqhead.Util;
import com.gimi.cloud.jimi.dto.prod.ProdBrandDTO;
import com.gimi.cloud.jimi.manage.service.prod.BrandService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.MultiValueMap;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.support.StandardMultipartHttpServletRequest;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.InputStream;
import java.util.*;

/**
 * Created by lixj on 2017/12/19.
 */
@RestController
@RequestMapping(value = "brand")
public class BrandController {
    private static Logger logger = LoggerFactory.getLogger(BrandController.class);
    private final BrandService brandService;

    @Autowired
    public BrandController(BrandService brandService){
        this.brandService = brandService;
    }

    /**
     * 商品品牌根据id查询操作
     * @param prodBrandDTO
     * @param request
     * @param response
     */
    @RequestMapping(value = "/list")
    public void brandList(@RequestBody ProdBrandDTO prodBrandDTO, HttpServletRequest request, HttpServletResponse response) {
        Response resp = new Response();
        List<ProdBrandDTO> list = brandService.getBrandList();
        resp.setRes_data(list);
        resp.setResponseCode(ResponseCodeEnum.SUCCESSED);
        Util.sendResponse(JsonUtil.toJson(resp), response);
    }

    /** 查询品牌分页列表 */
    @RequestMapping(value = "/pageList")
    public void pageList(@RequestBody ProdBrandDTO prodBrandDTO, HttpServletResponse response) {
        Response resp = new Response();
        Page<ProdBrandDTO> list = brandService.getBrandPageList(prodBrandDTO);
        resp.setRes_data(list);
        resp.setResponseCode(ResponseCodeEnum.SUCCESSED);
        Util.sendResponse(JsonUtil.toJSONStringWithDateFormatYYYYMMDDHHMMSS(resp), response);
    }

    /**
     * 商品品牌添加操作
     * @param prodBrandDTO
     * @param request
     * @param response
     */
    @RequestMapping(value = "/add")
    public void brandAdd(@RequestBody ProdBrandDTO prodBrandDTO, HttpServletRequest request, HttpServletResponse response) {
        Response resp = new Response();
        prodBrandDTO.setVersion(0);
        ResponseCodeEnum responseCodeEnum= brandService.brandAdd(prodBrandDTO);
        resp.setResponseCode(responseCodeEnum);
        Util.sendResponse(JsonUtil.toJson(resp), response);
    }

    /**
     * 商品品牌更新操作
     * @param prodBrandDTO
     * @param request
     * @param response
     */
    @RequestMapping(value = "/update")
    public void brandUpdate(@RequestBody ProdBrandDTO prodBrandDTO, HttpServletRequest request, HttpServletResponse response) {
        Response resp = new Response();
        ResponseCodeEnum responseCodeEnum= brandService.brandUpdate(prodBrandDTO);
        resp.setResponseCode(responseCodeEnum);
        Util.sendResponse(JsonUtil.toJson(resp), response);
    }

    /**
     * 商品品牌删除操作
     * @param prodBrandDTO
     * @param request
     * @param response
     */
    @RequestMapping(value = "/delete")
    public void brandDelete(@RequestBody ProdBrandDTO prodBrandDTO, HttpServletRequest request, HttpServletResponse response) {
        logger.info("删除商品品类模板，逻辑删除/启用，入参："+prodBrandDTO);
        Response resp = new Response();
        if(brandService.brandDelete(prodBrandDTO)){
            resp.setResponseCode(ResponseCodeEnum.SUCCESSED);
        }else{
            resp.setResponseCode(ResponseCodeEnum.PRODBRAND_IS_NOT_EXIT);
        }
        Util.sendResponse(JsonUtil.toJson(resp), response);
    }

    @RequestMapping(value = "/gotoEditProdBrand")
    public void gotoEditProdCatModel(@RequestBody ProdBrandDTO prodBrandDTO, HttpServletResponse response) {
        Response resp = new Response();
        ProdBrandDTO newprodBrandDTO=brandService.gotoEditProdBrand(prodBrandDTO);
        resp.setRes_data(newprodBrandDTO);
        resp.setResponseCode(ResponseCodeEnum.SUCCESSED);
        Util.sendResponse(JsonUtil.toJson(resp), response);
    }

    @RequestMapping(value = "/fileUpload")
    public void fileUpload(HttpServletRequest request, HttpServletResponse response) throws Exception {
        String url = "";
        StandardMultipartHttpServletRequest multipartHttpServletRequest = (StandardMultipartHttpServletRequest) request;
        MultiValueMap multipartFiles = multipartHttpServletRequest.getMultiFileMap();
        for(Object entry : multipartFiles.entrySet()){
            LinkedList<MultipartFile> lists = ((Map.Entry<String, LinkedList<MultipartFile>>) entry).getValue();
            for(MultipartFile file : lists){
                InputStream is = file.getInputStream();
                String uuId = UUIDUtil.get32UUID();
                String dirPath = "brand";
                String[] originalFilenames = file.getOriginalFilename().split("\\.");
                String fileName = uuId + "." + originalFilenames[originalFilenames.length-1];
                String result = AliOSSUtil.uploadFile(is,dirPath,fileName);
                String tempUrl = AliOSSUtil.getFileUrl(result,new Date(System.currentTimeMillis() + 1000 * 60 * 60 * 24 * 365 * 100));
                System.out.println("==========>上传结果：" + url);
                url += ","+tempUrl;
            }
        }

        Response resp = new Response();
        Map<String,String> data = new HashMap<>();
        data.put("url",url.equals("")?url:url.substring(1));
        resp.setRes_data(data);
        resp.setResponseCode(ResponseCodeEnum.SUCCESSED);
        Util.sendResponse(JsonUtil.toJson(resp), response);
    }
}
